﻿using Common.Model;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Serilog;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace Common
{
    /// <summary>
    /// 读取Appsetting配置信息
    /// </summary>
    public class AppSetting
    {
        #region public属性
        public string EqCode;
        #endregion
        #region 属性
        private IConfiguration Configuration;

        public ConcurrentBag<SettingMultipleModel> settingMultipleModels {  get; private set; } = new ConcurrentBag<SettingMultipleModel>();
        public ConcurrentBag<SettingSingleModel> settingSingleModels { get; private set; } = new ConcurrentBag<SettingSingleModel>();
        #endregion
        #region 单例
        public static AppSetting Instance = new Lazy<AppSetting>(() => new AppSetting()).Value;
        private AppSetting() { Init(); }
        #endregion

        private void Init()
        {
            Configuration = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).Add(
                                                       new JsonConfigurationSource 
                                                       {
                                                         Path = ConstString.Settingpath,
                                                         Optional = false,
                                                         ReloadOnChange = true
                                                       }).Build();
        }
        /// <summary>
        /// 读取多个重复子配置的配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public void ReadMultipleSubInfo<T>(string key)
        {
            List<T> result = new List<T>();
            try
            {
                if (Configuration is null)
                { 
                    return;
                }
                Configuration.Bind(key, result);
                SettingMultipleModel settingMultipleModel = new SettingMultipleModel();
                settingMultipleModel.SettingType = key;
                settingMultipleModel.ModelType = typeof(List<T>);
                settingMultipleModel.Values = JsonConvert.SerializeObject(result);
                settingMultipleModels.Add(settingMultipleModel);
            }
            catch (Exception ex) 
            {
                Log.Error($"读取配置信息异常{ex.Message}");
            }
        }

        public void ReadSingleSubInfo(string key,List<string> subKey,object instance)
        {
            try
            {
                if (Configuration is null)
                {
                    return;
                }
                SettingSingleModel settingSingleModel = new SettingSingleModel();
                settingSingleModel.SettingType = key;
                settingSingleModel.ModelType = instance.GetType();
                var ptroprtyies = settingSingleModel.ModelType.GetProperties();
                if (subKey is null)
                {
                    var value = Configuration[key];
                    if (instance is StringBuilder info)
                    {
                        info.Append(value);
                    }
                }
                else
                {
                    subKey.ForEach(x =>
                    {
                        var value = Configuration[key + ConstString.Colon + x];
                        var p = ptroprtyies.FirstOrDefault(p => p.Name == x);
                        p.SetValue(instance, value);
                    });
                }
                
                settingSingleModel.Values = JsonConvert.SerializeObject(instance);
                settingSingleModels.Add(settingSingleModel);
            }
            catch (Exception ex)
            {
                Log.Error($"读取单个配置信息异常{ex.Message}");
            }
        }



        public void GetEquipMentCodeSettingInfo()
        {
            var info = AppSetting.Instance.settingSingleModels.FirstOrDefault(s => s.SettingType == ConstString.EquipmentNumberKey);
            if (info is not null)
            {
                var value = JsonConvert.DeserializeObject<StringBuilder>(info.Values);
                if (value is not null)
                {
                    EqCode = value.ToString();
                }
                else
                {
                    Log.Error($"设备编号读取有误!");
                }
            }
            else
            {
                Log.Error($"请配置当前设备编号!");
            }
        }
    }
}
